<template>
  <el-card style="margin-bottom:20px;">
    <div class="user-bio">
      <el-form label-width="80px" :rules="rules" ref="form" :model="user" class="useritem">
        <el-form-item label="名称" prop="username">
          <el-input v-model="user.username" class="inputs"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="realName">
          <el-input v-model="user.realName" class="inputs"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email" @blur="sendEmail" class="inputs"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="user.phone" class="inputs"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">更改</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import PanThumb from "@/components/PanThumb";

export default {
  components: { PanThumb },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          realName: "",
          email: "",
          avatar: "",
          roleName: "",
          deptName: "",
          postName: "",
          phone: "",
          createTime: "",
        };
      },
    },
  },
  data() {
    return {
      rules: {
        username: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        realName: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 3, max: 64, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入电话号码", trigger: "blur" },
          { min: 11, max: 11, message: "长度在11", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    sendEmail: function () {
      var regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (this.user.email != "" && !regEmail.test(this.user.email)) {
        this.$message({
          message: "邮箱格式不正确",
          type: "error",
        });
        this.user.email = "";
      }
    },
    submitForm() {

       this.$refs.form.validate((valid) => {

      console.log(valid )

         if(valid){ 
           this.$emit("submitForm", this.user);
         }else{
           return false;
         }
       })
    },
  },
};
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
